﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using RevStampWrapper.Properties;

namespace RevStampWrapper {
    class Program {
        static void Main(string[] args) {
            string executable = Extract();
            if (executable != null)
                Run(executable, args);
        }

        static string Extract() {
            // define the location for and create the temp folder to contain our real binaries
            string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            string dirName = Path.Combine(Path.GetTempPath(), "RevStamp." + version);
            if (!Directory.Exists(dirName)) Directory.CreateDirectory(dirName);

            // define the full paths to our binaries
            string sharpSvnPath = Path.Combine(dirName, "SharpSvn.dll");
            string revStampCorePath = Path.Combine(dirName, "RevStampCore.exe");

            // Copy the assembly and exe to the temporary file
            try {
                if (!File.Exists(sharpSvnPath))
                    using (Stream outFile = File.Create(sharpSvnPath))
                        outFile.Write(Resources.SharpSvn, 0, Resources.SharpSvn.Length);
                if (!File.Exists(revStampCorePath))
                    using (Stream outFile = File.Create(revStampCorePath))
                        outFile.Write(Resources.RevStampCore, 0, Resources.RevStampCore.Length);
            }
            catch (Exception e) {
                Console.Write(e);
                return null;
            }

            return revStampCorePath;
        }

        static void Run(string executable, string[] args) {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = executable;
            p.StartInfo.Arguments = "";
            foreach (string arg in args) {
                p.StartInfo.Arguments += "\"" + arg + "\" ";
            }

            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            Console.Write(output);
        }
    }
}
